﻿using Vestras.StarCraft2.Luna.Parser;
using Vestras.StarCraft2.Luna.Parser.Ast;

namespace Vestras.StarCraft2.Luna.Validation {
    public sealed class StructTypeValidator : ITypeValidator {
        public System.Type[] Type {
            get {
                return new[] { typeof(Struct) };
            }
        }

        public bool Validate(object value) {
            if (value != null && value is Struct) {
                ErrorSink.UpdateChangeCollection();
                Struct s = value as Struct;
                bool childrenValid = true;
                foreach (Entity child in s.Children) {
                    ITypeValidator typeValidator = ValidationEntryPoint.GetTypeValidatorForType(new[] { child.GetType() });
                    if (typeValidator != null) {
                        bool valid = typeValidator.Validate(child);
                        if (!valid) {
                            childrenValid = false;
                        }
                    }
                }

                return childrenValid;
            }

            return false;
        }
    }
}
